<?php

/**
 * 客栈申请
 */
class innAdmAction extends globalAction {

    private $_inn, $innDao, $upload;
    
    public function _initialize() {
    
		parent::_initialize();
        parent::_loginRequire();

        //获取客栈ID
        $innId = intval($_REQUEST['id']);
        if(empty($innId))
            parent::_message('error', '客栈ID为空！', __GROUP__);
        //查找客栈详情，判断是否有权操作
        $this->innDao = D('Inn');
        $inn = $this->innDao
            ->field($this->innDao->_map_flip)
            ->where(array(
                $this->innDao->_map['id'] => $innId,
                $this->innDao->_map['uid'] => __USERID__
            ))
            ->find();
        if(empty($inn))
            parent::_message('error', '您无权管理此客栈！');
        else {
            $this->_inn = $inn;
            $this->assign('inn',$this->_inn);
        }
        //准备接收上传文件
        import('ORG.Net.UploadFile');
        $this->upload = new UploadFile();
        $this->upload->maxSize = 1024*1024;      //1M
        $this->upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');
        $this->upload->savePath =  UPLOAD_PATH;  //文件保存目录
        $this->upload->autoSub = true;           //自动创建子目录
        $this->upload->subType = 'date';         //按日期创建子目录
        $this->upload->dateFormat = 'Ym/d';      //日期子目录规则（前后不加'/'）
        $this->upload->saveRule = 'uniqid';      //文件名生成规则
	}
    
    /**
     * 客栈管理首页（会员信息）
     */
    public function index() {
        $this->display();
    }
    
    
    /**
     * 客栈基本信息
     */
    public function base() {
        
        //客栈等级
        $InnClassDao = D('InnClass');
        $InnClassList = $InnClassDao
            ->field($InnClassDao->_map_flip)
            ->where(array( $InnClassDao->_map['menuId'] => 598 ))
            ->order($InnClassDao->_map['order'])
            ->select();
        $this->assign('InnClassList',makearray($InnClassList, 'id', 'name'));//标记客栈等级列表
        $this->assign('InnClass',$this->_inn['class']);//标记选中的客栈等级
        //客栈设施
        $InnFacilityDao = D('InnFacility');
        $InnFacilityList = $InnFacilityDao
            ->field($InnFacilityDao->_map_flip)
            ->where(array( $InnFacilityDao->_map['menuId'] => 599 ))
            ->order($InnFacilityDao->_map['order'])
            ->select();
        $this->assign('InnFacilityList',makearray($InnFacilityList, 'id', 'name'));//标记客栈设施列表
        $this->assign('InnFacility',preg_split('/,/', $this->_inn['facility']));//标记选中的客栈设施
        
        //渲染输出
        $this->display();
    }
    
    /**
     * 客栈图集
     */
    public function images() {
        //客栈图集
        $InnImagesDao = D('InnImages');
        $InnImagesList = $InnImagesDao
            ->field($InnImagesDao->_map_flip)
            ->where(array(
                $InnImagesDao->_map['menuId'] => 607,
                $InnImagesDao->_map['innId'] => $this->inn['id'],
            ))
            ->order($InnImagesDao->_map['order'])
            ->select();
        $this->assign('InnImagesList', $InnImagesList);
        
        //渲染输出
        $this->display();
    }
    
    /**
     * Ajax上传客栈图集
     */
    public function upimages() {
        //处理文件上传
        if(!$this->upload->upload()) {
            die('错误提示：文件上传失败！');
        } else {
            $InnImagesDao = D('InnImages');
            $info =  $this->upload->getUploadFileInfo();
            foreach($info as $upinfo) {
                $picpath = strtr(UPLOAD_PATH, array('.'=>'')) . $upinfo['savename'];
                //保存新图片
                $data = array(
                    $InnImagesDao->_map['path'] => $picpath,
                    $InnImagesDao->_map['innId'] => $this->inn['id'],
                );
                if($InnImagesDao->create($data))
                    if($picid = $InnImagesDao->add())
                        die(json_encode(array(
                            "picid" => $picid,
                            "picpath" => $picpath
                        )));
            }
        }
        die('错误提示：文件保存出错！');
    }
    
    /**
     * Ajax删除客栈图集
     */
    public function delimages() {
        $imgid = intval($_POST['imgid']);
        if(empty($imgid)) die('没有指定要删除的图片！');
        
        $InnImagesDao = D('InnImages');
        $InnImagesRow = $InnImagesDao
            ->field($InnImagesDao->_map_flip)
            ->where(array(
                $InnImagesDao->_map['menuId'] => 607,
                $InnImagesDao->_map['innId'] => $this->inn['id'],
                $InnImagesDao->_map['id'] => $imgid,
            ))
            ->find();
        if(empty($InnImagesRow)) die('没有找到要删除的图片！');
        
        @unlink('.'.$InnImagesRow['path']);
        $InnImagesDao->where(array($InnImagesDao->_map['id'] => $imgid))->delete();
        die('1');
    }
    
    /**
     * 集中处理表单提交
     */
    public function submit() {
        if(!$this->isPost()) parent::_message('error', '只接受POST数据！', $_SERVER['HTTP_REFERER']);
        //保存客栈基本信息
        if($_POST['baseSubmit']) {
            $_POST['facility'] = ','.implode(',', $_POST['facility']);
        }
        //保存客栈详情
        elseif ($_POST['detailsSubmit']) {
            //处理文件上传
            if(!$this->upload->upload()) {
                //parent::_message('error', $upload->getErrorMsg());
            } else {
                $info =  $this->upload->getUploadFileInfo();
                foreach($info as $upinfo) {
                    //保存新图片
                    $fieldName = 'pic'.(intval($upinfo['key'])+1);
                    $_POST[$fieldName] = strtr(UPLOAD_PATH, array('.'=>'')) . $upinfo['savename'];
                    //删除老图片
                    if(!empty($this->inn[$fieldName]))
                        @unlink( $this->inn[$fieldName] );
                }
            }
        }
        //保存会员信息
        elseif($_POST['indexSubmit']) {
            //处理证件上传
            if(!$this->upload->upload()) {
                //parent::_message('error', $this->upload->getErrorMsg(), $_SERVER['HTTP_REFERER'], 3);
            } else {
                $info =  $this->upload->getUploadFileInfo();
                foreach($info as $upinfo) {
                    $path = strtr(UPLOAD_PATH, array('.'=>'')) . $upinfo['savename'];
                    if($upinfo['key'] == 0) {
                        $_POST['idcard_pic'] = $path;
                        //删除老图片
                        if(!empty($this->inn['idcard_pic']))
                            @unlink( '.'.$this->inn['idcard_pic'] );
                    } elseif($upinfo['key'] == 1) {
                        $_POST['biz_pic'] = $path;
                        //删除老图片
                        if(!empty($this->inn['biz_pic']))
                            @unlink( '.'.$this->inn['biz_pic'] );
                    }
                }
            }
        }

        if($this->innDao->create()) {
            if(false !== $this->innDao->save()) {
                parent::_message('success', '保存成功', $_SERVER['HTTP_REFERER'], 3);
            } else {
                parent::_message('error', '保存失败！请联系管理员！', $_SERVER['HTTP_REFERER']);
            }
        } else {
            parent::_message('error', implode('<br />', $this->innDao->getError()), $_SERVER['HTTP_REFERER']);
        }
    }
}